package com.pm.controller.file;

import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.file.DailyNews;
import com.pm.entity.po.security.User;
import com.pm.service.file.DailyFileTypeService;
import com.pm.service.file.DailyNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "日常工作动态")
@UserLoginToken
@RestController
@RequestMapping("/dailyNews")
public class DailyNewsController {
    @Autowired
    DailyFileTypeService dailyFileTypeService;
    @Autowired
    DailyNewsService dailyNewsService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "添加日常工作动态")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody DailyNews dailyNews, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = dailyNewsService.addDailyNews(dailyNews, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("修改接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "修改日常工作动态")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(@RequestBody DailyNews dailyNews, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = dailyNewsService.updateDailyNews(dailyNews, user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("查询日常工作动态")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<DailyNews>> getList(@RequestBody DailyNews dailyNews) {
        List<DailyNews> result = dailyNewsService.getList(dailyNews);
        Response<List<DailyNews>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("查询日常工作动态")
    @PostMapping("/load")
    @ResponseBody
    public Response<DailyNews> lode(@RequestBody DailyNews dailyNews) {
        if (dailyNews.getId() == null) {
            return new Response<>(ExpCodeState.param_exception);
        }
        DailyNews result = dailyNewsService.load(dailyNews);
        Response<DailyNews> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("删除日常工作动态")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除日常工作动态")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> deleteDailyNews(DailyNews dailyNews) {
        Integer res = dailyNewsService.remove(dailyNews);
        if (res > 0) {
            return new Response<>(ExpCodeState.remove_data_success);
        } else {
            return new Response<>(ExpCodeState.remove_data_failed);
        }
    }

}
